package com.renyi.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MybatisService;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class DoctorOperationController extends BaseController {

	@Autowired
	private MybatisService mybatisService;

	// 医生-获取已关闭咨询列表
	@RequestMapping("getDoctorRecordList")
	public void getDoctorRecordList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getDoctorRecordListSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getDoctorRecordListCountSQL", pMap);
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取已关闭咨询详情
	@RequestMapping("getDoctorRecordDetail")
	public void getDoctorRecordDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";
			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("咨询详情编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);

			List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getDoctorRecordDetailSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getDoctorRecordDetailCountSQL", pMap);

			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(list);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取粉丝
	@RequestMapping("getFans")
	public void getFans(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getFansSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取咨询订单
	@RequestMapping("getRecordOrder")
	public void getRecordOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getRecordOrderSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取诊所详情
	@RequestMapping("/getClinicDetail")
	public void getClinicDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getClinicDetailSQL", pMap);

			Map<String, Object> dataMap = new HashMap<String, Object>();
			Map<String, Object> mapId = null;
			if (data != null && data.size() > 0) {
				mapId = data.get(0);

			}

			if (data.size() > 0) {
				result.setCode(0);
				result.setMsg("");
				;// //错误消息
				result.setData(mapId);
			} else {
				result.setCode(-1);
				result.setMsg("");
				;// //错误消息
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-更新诊所详情
	@RequestMapping("/updateClinicDetail")
	public void updateClinicDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String detailID = paramMap.get("detailID") != null ? paramMap.get("detailID").toString() : "";
			String sign = paramMap.get("sign") != null ? paramMap.get("sign").toString() : "";
			String speciality = paramMap.get("speciality") != null ? paramMap.get("speciality").toString() : "";
			String education = paramMap.get("education") != null ? paramMap.get("education").toString() : "";
			String award = paramMap.get("award") != null ? paramMap.get("award").toString() : "";

			if ("".equals(detailID) || "".equals(sign) || "".equals(speciality) || "".equals(education)
					|| "".equals(award)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("detailID", detailID);
			pMap.put("sign", sign);
			pMap.put("speciality", speciality);
			pMap.put("education", education);
			pMap.put("award", award);
			int key = (Integer) this.mybatisService.updateIbatisObject("updateClinicDetailSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取银行卡信息
	@RequestMapping("/getBankCard")
	public void getBankCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getBankCardSQL", pMap);

			Map<String, Object> dataMap = new HashMap<String, Object>();
			Map<String, Object> mapId = null;
			if (data != null && data.size() > 0) {
				mapId = data.get(0);
				result.setData(mapId);
			}

			result.setCode(0);
			result.setMsg("");
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-更新银行卡信息
	@RequestMapping("/updateBankCard")
	public void updateBankCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String name = paramMap.get("name") != null ? paramMap.get("name").toString() : "";
			String idCard = paramMap.get("idCard") != null ? paramMap.get("idCard").toString() : "";
			String bankCard = paramMap.get("bankCard") != null ? paramMap.get("bankCard").toString() : "";
			String bank = paramMap.get("bank") != null ? paramMap.get("bank").toString() : "";

			if ("".equals(bank)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号户参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("name", name);
			pMap.put("idCard", idCard);
			pMap.put("bankCard", bankCard);
			pMap.put("bank", bank);
			int key = 0;
			long isExist = this.mybatisService.findIbatisListCount("getBankCardIDSQL", pMap);
			if (isExist < 1) {
				key = (Integer) this.mybatisService.insertIbatisObject("insertBankCardSQL", pMap);
			} else {
				key = (Integer) this.mybatisService.updateIbatisObject("updateBankCardSQL", pMap);

			}
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取结算信息
	@RequestMapping("/getSettlement")
	public void getSettlement(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getSettlementSQL", pMap);

			result.setCode(0);
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-更新结算信息
	@RequestMapping("/submitSettlement")
	public void submitSettlement(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String money = paramMap.get("money") != null ? paramMap.get("money").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("money", money);
			int key = 0;
			long isExist = this.mybatisService.findIbatisListCount("getSettlementIDSQL", pMap);
			if (isExist < 1) {
				key = (Integer) this.mybatisService.insertIbatisObject("submitSettlementSQL", pMap);
				if (key > 0) {
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("");
				}
			} else {
				result.setCode(1);
				result.setMsg("还有结算未完成");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取证件图片地址
	@RequestMapping("/getCredUrl")
	public void getCredUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getCredUrlSQL", pMap);
			if (data.size() > 0) {
				result.setData(data.get(0));
			}

			result.setCode(0);

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-更新证件图片地址
	@RequestMapping("/setCredUrl")
	public void setCredUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String creFirstUrl = paramMap.get("creFirstUrl") != null ? paramMap.get("creFirstUrl").toString() : "";
			String creSecondUrl = paramMap.get("creSecondUrl") != null ? paramMap.get("creSecondUrl").toString() : "";
			String carFrontUrl = paramMap.get("carFrontUrl") != null ? paramMap.get("carFrontUrl").toString() : "";
			String carBackUrl = paramMap.get("carBackUrl") != null ? paramMap.get("carBackUrl").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			// 图片地址--身份证正反面、两个执照
			pMap.put("creFirstUrl", creFirstUrl);
			pMap.put("creSecondUrl", creSecondUrl);
			pMap.put("carFrontUrl", carFrontUrl);
			pMap.put("carBackUrl", carBackUrl);
			Integer key = (Integer) this.mybatisService.updateIbatisObject("setCredUrlSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取药品列表
	@RequestMapping("getDoctorMedicineList")
	public void getMedicineList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String typeID = paramMap.get("typeID") != null ? paramMap.get("typeID").toString() : "";

			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);
			pMap.put("typeID", typeID);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getDoctorMedicineListSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getDoctorMedicineListCountSQL", pMap);
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

}
